#!usr/bin/env python
# -*-coding:utf-8 -*-
"""
数据源：巨潮 -> 市场资讯 -> 上市公司 -> 公司资讯 -> 股本结构
爬取类目：股本结构
股票类型不包括港股（港股没有最新资料）
url_sample: http://www.cninfo.com.cn/information/stockstructure/szmb000001.html
"""

import sys
from copy import deepcopy

from company_crawler import CompanyCrawler
from company_crawler import CompanyInfoUtil
from xtls.logger import get_logger
from bs4 import BeautifulSoup
from xtls.timeparser import now

reload(sys)
sys.setdefaultencoding('utf-8')

CATEGORY = 'stockstructure'


def get_single_result():
    RESULT_SINGLE = {
        'changeDate': '',
        'changeReason': '',
        'outstandShares': {
            'value': '',
            'detail': {
                'commonShares': '',
                'BShares': '',
                'foreignShares': '',
                'otherOutstand': '',
            },
        },
        'limitShares': {
            'value': '',
            'detail': {
                'stateShares': '',
                'otherDomesticShares': '',
                'foreignHoldShares': '',
                'allotLegalShares': '',
            },
        },
        'notOutstandShares': {
            'value': '',
            'detail': {
                'stateShares': '',
                'domensticLegalShares': '',
                'foreignLegalShares': '',
                'naturalShares': '',
                'otherNotOutstandShares': ''
            },
        },
        'totalShares': '',
    }
    return RESULT_SINGLE


DETAIL_MAPPING = {
    u'变动日期': 'changeDate',
    u'变动原因': 'changeReason',
    u'已流通股份': 'outstandShares',
    u'人民币普通股': 'commonShares',
    u'境内上市外资股（B股）': 'BShares',
    u'境外上市外资股': 'foreignShares',
    u'其他流通股': 'otherOutstand',
    u'流通受限股份': 'limitShares',
    u'国有股': 'stateShares',
    u'国有股以外的内资股': 'otherDomesticShares',
    u'外资持股': 'foreignHoldShares',
    u'配售法人股': 'allotLegalShares',
    u'未流通股份': 'notOutstandShares',
    u'境内法人持股': 'domensticLegalShares',
    u'境外法人持股': 'foreignLegalShares',
    u'自然人持股': 'naturalShares',
    u'其他未流通股': 'otherNotOutstandShares',
    u'总股本': 'totalShares',
}


def get_result_format():
    RESULT_FORMAT = {
        'stockStruct': [get_single_result() for _ in xrange(3)]
    }
    return RESULT_FORMAT


# KEY_LABEL_MAPPING = {
#     u'变动日期': '',
#     u'变动原因': '',
#     u'已流通股份': '_out', #outstanding
#     u'流通受限股份': '_limit',
#     u'未流通股份': '_notOut', #not outstanding
#     u'总股本': '',
# }

FIRST_TITLE = {u'变动日期', u'变动原因', u'总股本', u'已流通股份', u'流通受限股份', u'未流通股份'}
HAS_SUBTITLE = {u'已流通股份', u'流通受限股份', u'未流通股份'}


class CompanyStockStruc(CompanyCrawler):
    def __init__(self, company_info_util_instance):
        super(CompanyStockStruc, self).__init__(company_info_util_instance=company_info_util_instance)
        self.detail_mapping = self.company_info.detail_mapping

    def find_company_info_from(self, url):

        result = get_result_format()

        html = self.get(url)
        soup = BeautifulSoup(html, 'html5lib')

        if soup.find('img', src='/error.jpg'):
            self.logger.info(u'undealed: not find the company %s' % url)
            return result

        tr_list = soup.select('div.clear tr')

        if not tr_list:
            self.logger.info(u'undealed: the page format has been changed %s' % url)
            return result

        for tr in tr_list:
            tds = tr.find_all('td')

            if len(tds) != 4:
                self.logger.warn(u'find the different page format %s' % url)
                continue

            title = tds[0].getText()
            key = self.detail_mapping.get(title, None)

            if not key:
                self.logger.info(u'undealed: Undefined %s' % title)
                continue

            if title not in FIRST_TITLE:
                for index, td in enumerate(tds[1:]):
                    result['stockStruct'][index][parent_key]['detail'][key] = td.getText()

            elif title in HAS_SUBTITLE:
                parent_key = key
                for index, td in enumerate(tds[1:]):
                    result['stockStruct'][index][key]['value'] = td.getText()
            else:
                parent_key = key

                for index, td in enumerate(tds[1:]):
                    result['stockStruct'][index][key] = td.get_text()
        return result

        # def find_company_info_from(self, url):
        #     global RESULT_FORMAT
        #     result = dict()
        #     result['stockStruct'] = [{} for _ in xrange(3)]
        #     html = self.get(url)
        #     soup = BeautifulSoup(html)
        #     if soup:
        #         tr_list = soup.select('div.clear tr')
        #         if tr_list:
        #             for tr in tr_list:
        #                 tds = tr.find_all('td')
        #                 if len(tds) != 4:
        #                     self.logger.warn(u'find the different page format %s' % url)
        #                     continue
        #                 title = tds[0].getText()
        #                 key = self.detail_mapping.get(title, None)
        #                 if key:
        #                     global KEY_LABEL_MAPPING
        #                     if title in KEY_LABEL_MAPPING.keys():
        #                         key_label = KEY_LABEL_MAPPING.get(title)
        #                     else:
        #                         key = key + key_label
        #                     for index, td in enumerate(tds[1:]):
        #                         result['stockStruct'][index][key] = td.getText()
        #                 else:
        #                     self.logger.info(u'undealed: Undefined %s' % title)
        #         else:
        #             self.logger.info(u'undealed: the page format has been changed %s' % url)
        #     else:
        #         self.logger.info(u'undealed: not find the company %s' % url)
        #
        #     return result
        #
    def save(self, data):
        data['updateTime'] = now()
        for key, value in data.items():
            if type(value) == list:
                for element in value:
                    if type(element) == dict:
                        for sub_key, sub_value in element.items():
                            if type(sub_value) == dict:
                                for ssub_key, ssub_value in sub_value.items():
                                    print '{}: {}'.format(ssub_key, ssub_value)
                                print '-=' * 15
                            else:
                                print '{}: {}'.format(sub_key, sub_value)
                    print '-' * 50
            else:
                print '{}: {}'.format(key, value)
        print "=" * 70


def main():
    global CATEGORY
    global DETAIL_MAPPING
    from_company_info = CompanyInfoUtil(category=CATEGORY,
                                        detail_mapping=DETAIL_MAPPING)
    from_company_info.set_logger(get_logger(__file__))

    try:
        CompanyStockStruc(from_company_info).run()
    except:
        from_company_info.logger.warn('%s shuttdown' % from_company_info.category)


if __name__ == '__main__':
    main()
